package per.hnvcam.searchext.text.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import per.hnvcam.searchext.text.WordCounter;

/**
 * Created by IntelliJ IDEA.
 * User: violon
 * Date: 5/3/11
 * Time: 1:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class WordCounterImpl implements WordCounter {

   private Map<String, Integer> wordHolder;

   public WordCounterImpl() {
      wordHolder = new HashMap<String, Integer>();
   }

   public void count(String word) {
      Integer count = wordHolder.get(word);
      if (count != null) {
         wordHolder.put(word, ++count);
      }
      else {
         wordHolder.put(word, 1);
      }
   }

   public int resultOf(String word) {
      Integer count = wordHolder.get(word);
      return count != null ? count.intValue() : 0;
   }

   @Override
   public Iterator<String> iterator() {
      return wordHolder.keySet().iterator();
   }
}
